package com.jhd.jiangxi.recycle.widget;


import android.content.Context;
import android.graphics.Canvas;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

/**
 * Created by wuzd on 2018/5/17.
 */

public class RecycleItemDecoration extends DividerItemDecoration {
    private int mDividerMargin;

    public RecycleItemDecoration(Context context, int orientation, boolean isDrawLast, int margin) {
        super(context, orientation, isDrawLast);
        this.mDividerMargin = margin;
    }

    @Override
    public void drawVertical(Canvas c, RecyclerView parent) {
        super.drawVertical(c, parent);
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount;
        if (mIsDrawLast) {
            childCount = parent.getChildCount();
        } else {
            childCount = parent.getChildCount() - 1;
        }
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = parent.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            if (i == childCount - 1) {
                mDivider.setBounds(left, top, right, bottom);
            } else {
                mDivider.setBounds(left + mDividerMargin, top, right, bottom);
            }
            mDivider.draw(c);
        }

    }
}
